import React, {Component} from 'react'
import EosChainClient from "../../eos-dg-api/EosChainClient";
import EosWalletClient from "../../eos-dg-api/EosWalletClient"
import EosAppClient from "../../eos-dg-api/EosAppClient";
import StorageUtil from "../../eos-dg-api/platform/StorageUtil";
import {Button} from "primereact/button";

let zuowen123123_PrivateKey = '5KHZLf2R26fvdKz3nT22PTYjYb9M1jUwcFqagDKKagiNpze9Lis';
let yuanyouliaaa_PrivateKey = '5J31NJnshp9iA1d3Bu2gAuwrUUJFAYw2FxHYG7kDctjUdSnphVy';
let zuowen123123_PublicKey = 'EOS8SFR2nmePUrbyGnE4rRd4ZtwTXkTyWuczn25umyMafvx8XbmcJ';
let yuanyouliaaa_PublicKey = 'EOS5rLDo14AQuUPn9aGgBzvjGjVvbwn5JwdcY9bpGWq6ZFdW1ZSja';
export default class TestEg extends Component {
    constructor() {
        super();
        this.state = {}
    }

    renderTitle(name){
        return <h3>{name}</h3>
    }

    renderBtn(key,name,enable=true){
        if(enable){
            return <Button label={name} className="p-button-success" style={{margin:"0 10px 10px 0"}} onClick={this.onPress.bind(this,key)}/>
        }else{
            return <Button label={name} className="p-button-success" disabled style={{marginRight:"10px"}}/>
        }
    }

    testLog = (key,data) => {
        console.log('Test '+key+' : '+JSON.stringify(data))
    };

    developing = () => {
        // EasyToast.show('该接口正在封装中，请先使用假数据');
    };

    onPress(key){
        //钱包wallet
        if(key==='walletList'){
            EosWalletClient.list((data)=>{
                this.testLog(key,data)
            })
        }else if(key==='walletDel'){
            let payload = {
                publicKey:'123',
            };
            EosWalletClient.del(payload,(data)=>{
                this.testLog(key,data)
            })
        }else if(key==='walletUpdate'){
            let payload = {
                name:'wallet1',
                publicKey:'123',
                password:'2468'
            };
            EosWalletClient.update(payload,(data)=>{
                this.testLog(key,data)
            })
        }else if(key==='walletAdd'){
            let payload = {
                name:'wallet1',
                publicKey:'123',
                password:'12345678'
            };
            EosWalletClient.create(payload,(data)=>{
                this.testLog(key,data)
            })
        }else if(key==='getDefaultWallet'){
            EosWalletClient.getDefaultWallet((data)=>{
                this.testLog(key,data)
            })
        }else if(key==='updateDefaultWallet'){
            let payload = {
                name:'wallet1',
                publicKey:'123',
                password:'12345678'
            };
            EosWalletClient.updateDefaultWallet(payload,(data)=>{
                this.testLog(key,data)
            })
        }

        //账号account
        if(key==='existAccount'){
            let accountName = 'zuowen123123';
            EosChainClient.existAccount(accountName,(data)=>{
                this.testLog(key,data)
            });
        }else if(key==='getAccount'){
            let accountName = 'zuowen123123';
            EosChainClient.getAccount(accountName,(data)=>{
                this.testLog(key,data)
            });
        }else if(key==='createAccount'){
            let payload = {
                accountName:'zuowen123124',
                ownerPublicKey:zuowen123123_PublicKey,
                activePublicKey:zuowen123123_PublicKey,
                code:'123'
            };
            EosAppClient.accountCreate(payload,(data)=>{
                this.testLog(key,data)
            });
        }else if(key === 'bidList'){
            this.developing();
        }else if(key === 'childAccountList'){
            this.developing();
        }else if(key === 'permissionList'){
            this.developing();
        }else if(key === 'actionList'){
            this.developing();
        }else if(key === 'bid'){
            this.developing();
        }else if(key === 'childAccountCreate'){
            this.developing();
        }

        //秘钥key
        if(key === 'generatePk'){
            EosChainClient.randomPrivateKey((data)=>{
                this.testLog(key,data)
            })
        }else if(key === 'priToPub'){
            EosChainClient.privateToPublic(zuowen123123_PrivateKey,(data)=>{
                this.testLog(key,data)
            })
        }else if(key === 'checkPriKey'){
            EosChainClient.checkPrivateKey(zuowen123123_PrivateKey,(data)=>{
                this.testLog(key,data)
            })
        }else if(key === 'checkPubKey'){
            EosChainClient.checkPublicKey(zuowen123123_PublicKey,(data)=>{
                this.testLog(key,data)
            })
        }else if(key === 'getKeyAccounts'){
            EosChainClient.getKeyAccounts(zuowen123123_PublicKey,(data)=>{
                this.testLog(key,data)
            })
        }

        //投票vote
        if(key==='productRecommend'){
            let payload = {
                first:0,
                pageSize:10,
            };
            EosAppClient.producerRecommendList(payload,(data)=>{
                this.testLog(key,data)
            });
        }else if(key === 'productRank'){
            let payload = {
                first:0,
                pageSize:10,
            };
            EosAppClient.producerRankList(payload,(data)=>{
                this.testLog(key,data)
            });
        }else if(key === 'productVoted'){
            let payload = {
                first:0,
                pageSize:10,
                account:'zuowen123123'
            };
            EosAppClient.producerVotedList(payload,(data)=>{
                this.testLog(key,data)
            });
        }else if(key === 'productVote'){

        }else if(key === 'productUnVote'){

        }

        //资源resource
        if(key==='sellarm'){

        }else if(key === 'buyarm'){

        }else if(key === 'delegatecpu'){

        }else if(key === 'undelegatecpu'){

        }else if(key === 'delegatenet'){

        }else if(key === 'undelegatenet'){

        }else if(key === 'armprice'){

        }else if(key === 'cpuprice'){

        }else if(key === 'netprice'){

        }

        //代币token
        if(key==='sendToken'){
            let privateKey = '5KHZLf2R26fvdKz3nT22PTYjYb9M1jUwcFqagDKKagiNpze9Lis';
            EosChainClient.transfer("eosio.Token", 'zuowen123123', 'yuanyouliaaa', "0.0001 EOS", 'web test1', privateKey, true, (r) => {
                console.log(r);
            })

        }else if(key === 'receiveToken'){

        }else if(key === 'accountTokens'){
            this.developing();
        }else if(key === 'tokenTradeList'){
            this.developing();
        }

        //地址本address
        if(key === 'addressList'){
            EosAppClient.addressList((data)=>{
                this.testLog(key,data)
            })
        }else if(key === 'addressAdd'){
            let payload={
                id:1,
                name:'zuowen123123',
                des:'friend'
            };
            EosAppClient.addressAdd(payload,(data)=>{
                this.testLog(key,data)
            })
        }else if(key === 'addressDel'){
            let itemKey='id';
            let itemValue = 1;
            EosAppClient.addressDel(itemKey,itemValue,(data)=>{
                this.testLog(key,data)
            })
        }else if(key === 'addressUpdate'){
            let itemKey='id';
            let itemValue = 1;
            let payload={
                id:1,
                name:'zuowen123123',
                des:'workmate'
            };
            EosAppClient.addressUpdate(itemKey,itemValue, payload,(data)=>{
                this.testLog(key,data)
            })
        }else if(key === 'addressDetail'){
            let itemKey='id';
            let itemValue = 1;
            EosAppClient.addressDetail(itemKey,itemValue,(data)=>{
                this.testLog(key,data)
            })
        }else if(key === 'addressClear'){
            StorageUtil.del('AddressList1',(data)=>{
                this.testLog(key,data)
            })
        }

        //行情market
        if(key==='marketList'){
            EosAppClient.tokenMarketList((data)=>{
                this.testLog(key,data)
            });
        }else if(key === 'marketDetail'){
            let markId = 1;
            EosAppClient.tokenMarketDetail(markId,(data)=>{
                this.testLog(key,data)
            });
        }

        //应用dapp
        if(key==='dappList'){
            EosAppClient.dappList((data)=>{
                this.testLog(key,data)
            });
        }else if(key === 'dappBanner'){
            EosAppClient.dappBanner((data)=>{
                this.testLog(key,data)
            });
        }

        //EOS公链chain
        if(key==='getInfo'){
            EosChainClient.getInfo((data)=>{
                this.testLog(key,data)
            });
        }else if(key === 'getGlobalInfo'){
            EosChainClient.getGlobalInfo((data)=>{
                this.testLog(key,data)
            });
        }else if(key === 'getBlock'){
            let blockId = 1000;
            EosChainClient.getBlock(blockId,(data)=>{
                this.testLog(key,data)
            });
        }
    }

    render() {
        return (
            <div>
                {/*<Account/>*/}

                <h3 className="text-center">API Test 点击按钮，在控制台可查看数据</h3>

                <div className="ui-g-12">
                    <div className="card ">
                        {this.renderTitle('钱包wallet')}
                        {this.renderBtn('walletList','钱包list')}
                        {this.renderBtn('walletDel','钱包delete')}
                        {this.renderBtn('walletUpdate','钱包update')}
                        {this.renderBtn('walletAdd','钱包add')}
                        {this.renderBtn('getDefaultWallet','get默认钱包')}
                        {this.renderBtn('updateDefaultWallet','set默认钱包')}
                    </div>
                </div>

                <div className="ui-g-12">
                    <div className="card">
                        {this.renderTitle('账号account')}
                        {this.renderBtn('existAccount','检测账号有效')}
                        {this.renderBtn('getAccount','获取账号')}
                        {this.renderBtn('createAccount','邀请码创建账号')}

                        {this.renderBtn('bidList','名字拍卖list',false)}
                        {this.renderBtn('childAccountList','子账号list',false)}
                        {this.renderBtn('permissionList','权限组list')}
                        {this.renderBtn('actionList','其他操作list',false)}

                        {this.renderBtn('bid','名字拍卖action',false)}
                        {this.renderBtn('childAccountCreate','子账号创建action',false)}
                    </div>
                </div>

                <div className="ui-g-12">
                    <div className="card">
                        {this.renderTitle('秘钥key')}
                        {this.renderBtn('generatePk','生成公私钥')}
                        {this.renderBtn('checkPriKey','检查私钥有效')}
                        {this.renderBtn('checkPubKey','检查公钥有效')}
                        {this.renderBtn('priToPub','私钥获取公钥')}
                        {this.renderBtn('getKeyAccounts','公钥对应的账号')}
                    </div>
                </div>

                <div className="ui-g-12">
                    <div className="card">
                        {this.renderTitle('投票vote')}
                        {this.renderBtn('productRecommend','超级节点推荐list')}
                        {this.renderBtn('productRank','超级节点排名list')}
                        {this.renderBtn('productVoted','超级节点已投list')}

                        {this.renderBtn('productVote','投票action',false)}
                        {this.renderBtn('productUnVote','撤销投票action',false)}
                    </div>
                </div>

                <div className="ui-g-12">
                    <div className="card">
                        {this.renderTitle('资源resource')}
                        {this.renderBtn('sellarm','买arm',false)}
                        {this.renderBtn('buyarm','卖arm',false)}
                        {this.renderBtn('delegatecpu','质押cpu',false)}
                        {this.renderBtn('undelegatecpu','赎回cpu',false)}
                        {this.renderBtn('delegatenet','质押net',false)}
                        {this.renderBtn('undelegatenet','赎回net',false)}

                        {this.renderBtn('armprice','arm价格',false)}
                        {this.renderBtn('cpuprice','cpu价格',false)}
                        {this.renderBtn('netprice','net价格',false)}
                    </div>
                </div>

                <div className="ui-g-12">
                    <div className="card">
                        {this.renderTitle('代币token')}
                        {this.renderBtn('sendToken','转账token')}
                        {/*{this.renderBtn('receiveToken','接受token')}*/}
                        {this.renderBtn('accountTokens','账号token列表',false)}
                        {this.renderBtn('tokenTradeList','token交易记录',false)}
                    </div>
                </div>

                <div className="ui-g-12">
                    <div className="card">
                        {this.renderTitle('地址本address')}
                        {this.renderBtn('addressList','地址本list')}
                        {this.renderBtn('addressAdd','地址本add')}
                        {this.renderBtn('addressDel','地址本delete')}

                        {this.renderBtn('addressUpdate','地址本update')}
                        {this.renderBtn('addressDetail','地址本detail')}
                        {this.renderBtn('addressClear','地址本清空')}
                    </div>
                </div>

                <div className="ui-g-12">
                    <div className="card">
                        {this.renderTitle('行情market')}
                        {this.renderBtn('marketList','行情list')}
                        {this.renderBtn('marketDetail','行情token详情')}
                    </div>
                </div>

                <div className="ui-g-12">
                    <div className="card">
                        {this.renderTitle('应用dapp')}
                        {this.renderBtn('dappList','dapp列表')}
                        {this.renderBtn('dappBanner','banner推荐')}
                    </div>
                </div>

                <div className="ui-g-12">
                    <div className="card">
                        {this.renderTitle('公链chain')}
                        {this.renderBtn('getInfo','公链信息')}
                        {this.renderBtn('getGlobalInfo','全网信息')}
                        {this.renderBtn('getBlock','某个区块')}
                    </div>
                </div>
            </div>
        )
    }

}


